<template>
  <div class="playerContent">
    <div class="title">
      <div class="line"></div>
      <div class="name">比赛管理</div>
      <div class="add" @click="handleAdd">新增比赛</div>
      <!-- <div class="btn" @click="handleExport">导出</div> -->
    </div>
    <div class="search">
      <div class="inputItem">
        <span>比赛名称</span>
        <common-input v-model="raceForm.name" placeholder="请输入比赛名称"></common-input>
      </div>
      <div class="inputItem">
        <span>比赛时间</span>
        <el-date-picker v-model="raceForm.timeRange" type="daterange" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd">
        </el-date-picker>
      </div>
      <div class="inputItem"></div>
      <div class="btns">
        <div class="btn searchBtn" @click="handleSearch">查询</div>
        <div class="btn resetBtn" @click="handleReset">重置</div>
        <div class="btn resetBtn" @click="handleDel">删除</div>
      </div>
    </div>
    <div class="content">
      <CommonTablePage style="flex: 1" :columns="raceColumns" :pagination="pageInfo" :tableData="tableData">
        <template #userList="{ row }">
          <div class="tagList">
            <el-tag class="tagName" v-for="(item, index) in row.negativeNames.split(',')">{{ item }}</el-tag>
          </div>
          <div class="tagList">
            <el-tag class="tagName" v-for="(item, index) in row.positiveNames.split(',')">{{ item }}</el-tag>
          </div>
        </template>
        <template #time="{ row }">
          <span>{{ row.startTime + '\n' + row.endTime }}</span>
        </template>
        <template #state="{ row }">
          <span>
            {{
              getTimeStatus(row.startTime, row.endTime) == '0' ? '未开始' : getTimeStatus(row.startTime, row.endTime) == '1'
                ? '进行中' : '已结束'
            }}
          </span>
        </template>
        <template #edit="{ row }">
          <span class="edit" @click="handleEdit(row)">编辑</span>
          <span class="edit" @click="handleJoin(row)">比赛结果填写</span>
          <span class="edit" @click="handleUser(row)">观赛人数填写</span>

        </template>
      </CommonTablePage>
    </div>
    <changeDialog v-if="changeDialog" :changeDialog.sync="changeDialog" :curRow="curRow" :playerList="playerList"
      :refereeList="refereeList" @makeSure="makeSure"></changeDialog>
    <reaceRefultDialog v-if="resultDialog" :resultDialog.sync="resultDialog" :curRow="curRow"
      @makeSure="makeSureResult" />
    <userDialog v-if="userDialogShow" :userDialogShow.sync="userDialogShow" @makeSure="makeUserSure" />
  </div>
</template>
<script>
import { raceColumns } from "./column.js";
import { viewMinix } from "@/minix/viewMinix.js";
import changeDialog from "@/views/raceManage/components/changeDialog";
import reaceRefultDialog from "./components/reaceRefultDialog.vue";
import userDialog from "./components/userDialog.vue";
import { getPlayerList, getRefereeList, addMatch, editMatch, getTablePage, editMatchResult, editMatchUsers } from "@/api/raceManage"
export default {
  components: {
    changeDialog,
    reaceRefultDialog,
    userDialog
  },
  mixins: [viewMinix],
  data() {
    return {
      userDialogShow: false,
      playerList: [],
      refereeList: [],
      curRow: {},
      changeDialog: false,
      resultDialog: false,
      raceColumns,
      tableData: [
        {
          id: 1,
          name: "张三",
          time: "2019-10-09 19:00:00 - 2019-10-09 20:00:00",
          team1: [
            {
              id: '1',
              name: '张三',
              score: '10'
            },
            {
              id: '2',
              name: '李四',
              score: '20'
            },
            {
              id: '3',
              name: '王五',
              score: '30'
            },
            {
              id: '4',
              name: '赵六',
              score: '40'
            },
            {
              id: '5',
              name: '孙七',
              score: '50'
            }
          ],
          team2: [
            {
              id: '6',
              name: '张三2',
              score: '60'
            },
            {
              id: '7',
              name: '李四2',
              score: '70'
            },
            {
              id: '8',
              name: '王五2',
              score: '80'
            },
            {
              id: '9',
              name: '赵六2',
              score: '90'
            },
            {
              id: '10',
              name: '孙七2',
              score: '100'
            }
          ],
          referee: ["李武"],
          raceArea: "篮球场",
          state: "0",
        },
      ],
      raceForm: {
        name: "",
        timeRange: "",
      },

      rows: [],
    };
  },
  created() {
    console.log(userDialog, 'userDialog');

    this.getTableData()
    this.getPlayerList()
    this.getRefereeList()
  },
  methods: {
    handleUser(row) {
      console.log(row, 'row');

      this.curRow = row
      this.userDialogShow = true
    },
    async makeUserSure(v) {
      console.log(v, 'v');
      console.log(this.curRow, 'this.curRow');
      let data = {
        matchId: this.curRow.matchId,
        audienceNumber: v
      }
      const res = await editMatchUsers(data)
      if (res.code == 200) {
        this.$message.success('填写成功')
        this.getTableData()
      }
      this.userDialogShow = false
    },
    async makeSureResult(v) {
      console.log(v);
      let data = []
      v.team1.forEach(item => {
        data.push({
          matchesId: v.matchId,
          playersId: item.id,
          score: item.score
        })
      })
      v.team2.forEach(item => {
        data.push({
          matchesId: v.matchId,
          playersId: item.id,
          score: item.score
        })
      })
      console.log(data, 'data');
      const res = await editMatchResult(data)
      this.resultDialog = false
      if (res.code == 200) {
        this.getTableData()
        this.$message({
          type: 'success',
          message: '填写成功',
        });
      }
    },
    getTimeStatus(startTime, endTime) {
      const currentTime = new Date(); // 获取当前时间
      const start = new Date(startTime); // 将 startTime 转换为 Date 对象
      const end = new Date(endTime); // 将 endTime 转换为 Date 对象

      if (currentTime < start) {
        return 0; // 当前时间在比赛开始前
      } else if (currentTime >= start && currentTime <= end) {
        return 1; // 当前时间在比赛时间范围内
      } else {
        return 2; // 当前时间在比赛结束后
      }
    },
    handleSearch() {
      this.getTableData()

    },
    handleReset() {
      this.raceForm.name = "";
      this.raceForm.time = ["", ""];
      this.getTableData()
    },
    async getPlayerList() {
      const { data } = await getPlayerList()
      console.log(data, 'playerList');
      this.playerList = data
    },
    async getRefereeList() {
      const { data } = await getRefereeList()
      console.log(data, 'getRefereeList');
      this.refereeList = data

    },
    async getTableData() {

      let data = {
        page: {
          ...this.pageInfo
        },
        param: {
          matchName: this.raceForm.name,

        }
      }
      const res = await getTablePage(data)
      console.log(res, 'rr');

      if (res.code == 200) {
        this.tableData = res.data.records
        this.pageInfo.total = res.data.total * 1
      }
    },

    selRows(rows) {
      this.rows = rows;
    },
    handleDel() {
      if (this.rows.length < 1) {
        return this.$message({
          type: "error",
          message: "请勾选球员信息!",
        });
      }
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => { });
    },
    handleAdd() {
      this.curRow = {};
      this.changeDialog = true;
    },
    handleEdit(row) {
      if (!this.isBeforeMatchStart(row.startTime, row.endTime)) {
        this.curRow = row;
        this.resultDialog = true;
      } else {
        return this.$message.error("当前比赛已经结束，无法填写");
      }
      this.curRow = row;
      this.changeDialog = true;
    },
    handleJoin(row) {
      if (this.isBeforeMatchStart(row.startTime, row.endTime)) {
        this.curRow = row;
        this.resultDialog = true;
      } else {
        return this.$message.error("当前比赛还未结束，无法填写");
      }

    },
    isCurrentTimeInRange(startTime, endTime) {
      const currentTime = new Date(); // 获取当前时间
      const start = new Date(startTime); // 将 startTime 转换为 Date 对象
      const end = new Date(endTime); // 将 endTime 转换为 Date 对象

      return currentTime >= start && currentTime <= end;
    },
    isBeforeMatchStart(startTime) {
      const currentTime = new Date(); // 获取当前时间
      const start = new Date(startTime); // 将 startTime 转换为 Date 对象

      return currentTime > start;
    },
    async makeSure(v) {
      if (v.matchId) {
        const res = await editMatch(v)
        if (res.code == 200) {
          this.getTableData()
          this.$message({
            type: 'success',
            message: '修改成功'
          })
          this.changeDialog = false
        } else {
          this.$message.error('添加失败')
        }
      } else {
        const res = await addMatch(v)
        if (res.code == 200) {
          this.getTableData()
          this.$message({
            type: 'success',
            message: '添加成功'
          })
          this.changeDialog = false
        } else {
          this.$message.error('添加失败')
        }
      }

    }
  },
};
</script>

<style lang="less" scoped>
.playerContent {
  height: 100%;
  display: flex;
  flex-direction: column;

  .title {
    display: flex;
    align-items: center;
    height: 70px;
    padding: 10px;
    padding-left: 30px;
    background-color: #fff;
    font-weight: 600;
    border-radius: 5px;
    margin-bottom: 15px;

    .line {
      height: 20px;
      background-color: #fc7414;
      width: 6px;
      margin-right: 10px;
    }

    .btn {
      width: 68px;
      height: 32px;
      display: flex;
      font-size: 14px;
      background-color: #fc6c09;
      border-radius: 4px;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #fff;
    }

    .add {
      margin-left: auto;

      margin-right: 16px;
      width: 68px;
      height: 32px;
      display: flex;
      font-size: 14px;
      background-color: #fc6c09;
      border-radius: 4px;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #fff;
    }
  }

  .search {
    height: 80px;
    margin-top: 15px;
    padding: 10px;
    padding-left: 30px;
    height: 80px;
    display: flex;
    align-items: center;
    background-color: #fff;

    .inputItem {
      display: flex;
      margin-right: 10px;

      span {
        width: 70px;
        display: flex;
        align-items: center;
      }
    }

    .btns {
      display: flex;

      .btn {
        width: 68px;
        height: 32px;
        display: flex;
        font-size: 14px;
        background-color: #fc6c09;
        border-radius: 4px;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #fff;
      }

      .searchBtn {
        margin-right: 16px;
      }

      .resetBtn {
        margin-right: 16px;
      }
    }
  }

  .content {
    flex: 1;
    margin-top: 20px;
    display: flex;
    background-color: #fff;
    padding: 15px 10px 10px 30px;

    .edit {
      color: #fc6c09;
      cursor: pointer;
      margin-right: 10px;
    }

    .tagList {
      display: flex;

      .tagName {
        min-width: 70px;
        margin-right: 8px;
        margin-bottom: 3px;
      }
    }
  }
}
</style>
